package org.news.web.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import com.dtflys.forest.annotation.Post;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.news.web.constant.UsersConstant;
import org.news.web.constant.UsersStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.UsersVo;
import org.news.web.domain.bo.UsersBo;
import org.news.web.service.IUsersService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户
 *
 * @author Gu Ci
 * @date 2024-03-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/users")
public class UsersController extends BaseController {

    private final IUsersService usersService;

    /**
     * 查询用户列表
     */
    @SaCheckPermission("web:users:list")
    @GetMapping("/list")
    public TableDataInfo<UsersVo> list(UsersBo bo, PageQuery pageQuery) {
        return usersService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询没有被停用的用户列表
     */
    @GetMapping("/NoTYlist")
    public R<List<UsersVo>> NoTYlist(String Key) {
        return R.ok(usersService.queryNoTYList(Key));
    }

    /**
     * 导出用户列表
     */
    @SaCheckPermission("web:users:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UsersBo bo, HttpServletResponse response) {
        List<UsersVo> list = usersService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户", UsersVo.class, response);
    }

    /**
     * 获取用户详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("web:users:query")
    @GetMapping("/{userId}")
    public R<UsersVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(usersService.queryById(userId));
    }

    /**
     * 新增用户
     */
    @SaCheckPermission("web:users:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UsersBo bo) {
        return toAjax(usersService.insertByBo(bo));
    }

    /**
     * 修改用户
     */
    @SaCheckPermission("web:users:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UsersBo bo) {
        return toAjax(usersService.updateByBo(bo));
    }

    /**
     * 删除用户
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("web:users:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(usersService.deleteWithValidByIds(List.of(userIds), true));
    }

    /**
     * 获取用户角色信息
     */
    @RequestMapping("/getUsersRoles")
    public R<List<UsersStatus>> UserROLES() {
        return R.ok(UsersConstant.UsersRoles());
    }

    /**
     * 获取省份信息
     */
    @RequestMapping("/getShengFen")
    public R<List<String>> ShengFen() {
        return R.ok(UsersConstant.ShenFen());
    }

    /**
     * 用户状态信息
     */
    @RequestMapping("/getUsersStatus")
    public R<List<UsersStatus>> UsersStatus() {
        return R.ok(UsersConstant.UserStatus());
    }

    /**
     * 重置密码
     */
    @PostMapping("/rePassWord")
    public R<Void> getrepassword(@RequestBody UsersBo bo){
       return toAjax(usersService.rePassWord(bo));
    }


    /**通过用户id创建作者
     *
     */
    @PostMapping("/createCreatorsByUserId")
    public R<Void> createCreatorsByUserId(@RequestBody long id){
        return toAjax(usersService.createCreatorsByUserId(id));
    }








}
